package com.nyist.controller;

import com.alibaba.fastjson.JSONObject;
import com.nyist.entity.User;
import com.nyist.mq.service.ProducerService;
import com.nyist.mq.service.ProducerTXService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 1.
 *
 * @author yangxiaogen
 * @date 2023/3/23 16:37
 */
@Slf4j
@RestController
@RequestMapping("/producer")
public class ProducerController {

    @Resource
    private ProducerService producerService;
    @Resource
    private ProducerTXService producerTXService;

    @PostMapping("/sendASync")
    public void sendASyncRocket(@Valid @RequestBody String message) {
        log.info("=====>>>c,tag=sendASync,入参为:{}", JSONObject.toJSONString(message));
        producerService.asyncSend(message);
    }

    @PostMapping("/send")
    public void sendRocket(@Valid @RequestBody String message) {
        log.info("=====>>>c,tag=send,入参为:{}", JSONObject.toJSONString(message));
        producerService.send(message);
    }

    @PostMapping("/syncSend")
    public SendResult syncSendRocket(@Valid @RequestBody String message) {
        log.info("=====>>>c,tag=syncSend,入参为:{}", JSONObject.toJSONString(message));
        return producerService.syncSend(message);
    }

    @PostMapping("/txSend")
    public TransactionSendResult txSendRocket(@Valid @RequestBody User user) {
        log.info("=====>>>c,tag=txSend,入参为:{}", JSONObject.toJSONString(user));
        TransactionSendResult transactionSendResult = producerTXService.sendHalfMessage(user);
        log.info("=====>>>c,tag=txSend,响应结果为:{}", JSONObject.toJSONString(transactionSendResult));
        return transactionSendResult;
    }

}
